<template>
    <div class="dragVerify" :style="{width: `${width}px`, height: `${height}px`, lineHeight: `${height}px`, borderColor: `${borderColor}`, background: `${bgColor}`, borderRadius: `${borderRadius}px`}">
        <!-- 滑块盒子 -->
        <div class="spout" ref="spout">
            <!-- 滑块 -->
            <div
                ref="slidingBlock"
                class="slidingBlock"
                :style="{ left: `${place}px`, width:`${blockWidth}px`, height: `${height}px`, lineHeight: `${height}px`, borderRadius: `${borderRadius}px`}"
                :class="place < distance ? 'unfinished' : 'complete'"
                @mousedown="mousedown($event)">
                <!-- 图标 -->
                <div 
                    class="blockIcon"
                    :style="{ width: `${blockIconWidth}px`, height: `${blockIconHeight}px`}">
                </div>
            </div>
            <!-- 绿色盒子 -->
            <div class="succeedBox" 
                :class="place >= distance ? 'succeedText' : ''" 
                :style="{ width: `${rectWidth}px`, height: `${height}px`, lineHeight: `${height}px`, borderRadius: `${borderRadius}px`}">
                <!-- 成功提示 -->
                <div class="successTip" v-if="place >= distance">
                    验证通过
                </div>
            </div>
        </div>
    </div>
</template>

<script>
export default {
    name: 'dragVerify',
    props: {
        width: {
           type: Number,
           default: 300,
        },
        height: {
           type: Number,
           default: 40
        },
        blockWidth: {
            type: Number,
            default: 40
        },
        blockHeight: {
            type: Number,
            default: 40
        },
        blockIconWidth: {
            type: Number,
            default: 40
        },
        blockIconHeight: {
            type: Number,
            default: 40
        },
        borderColor: {
            type: String,
            default: '#e1e1e1'
        },
        bgColor: {
            type: String,
            default: '#eeeeee'
        },
        borderRadius: {
            type: Number,
            default: 8
        }
    },
    data() {
        return {
            place: 0,
            callback: false,
            sliding: {
                isDown: true,
                X: 0 // 初始X值
            },
            move: {
                X: 0 // 移动X值
            },
            spoutW: 0,
            slidingBlockW: this.blockWidth,
            distance: 1 // 要移动的距离
        }
    },
    computed:{
        rectWidth(){
            return this.place + this.slidingBlockW;
        }
    },
    mounted() {},
    methods: {
        // 重置
        reset(){
            this.sliding.isDown = false
            this.place = 0
            this.callback = false
        },
		// 鼠标移动事件的回调方法
        mouseEventFn(e) {
			if (this.sliding.isDown) {
				this.move.X = e.x
				if (this.place >= this.distance) {
					this.place = this.distance
				} else if (this.place >= 0 && this.place < this.distance) {
					this.place = (this.move.X - this.sliding.X)
				}
				if (this.place <= 0) {
					this.place = 0
					document.removeEventListener('mousemove', null, false)
					return
				}
			}
			e.preventDefault()
        },
        // 鼠标事件
        mousedown(e) {
            // 计算移动距离
            if (this.place < this.distance) {
                // 按钮按下标志
                this.sliding.isDown = true
                // 整个盒子宽度
                this.spoutW = this.$refs.spout.offsetWidth
                // 滑块的宽度
                this.slidingBlockW = this.$refs.slidingBlock.offsetWidth
                // 剩下需移动距离
                this.distance = this.spoutW - this.slidingBlockW -0
            }
            // 滑块位置
            this.sliding.X = e.x
            // 添加鼠标的移动事件
            document.addEventListener('mousemove', this.mouseEventFn)
            // 松开鼠标
            document.onmouseup = e => {
				// 移除鼠标的移动事件
                document.removeEventListener('mousemove', this.mouseEventFn)
                if (this.place == this.distance) {
                    if(!this.callback) {
                        this.$emit('success', true)
                        this.callback = true
                    }
                } else {
                    this.callback = false
                    this.sliding.isDown = false
                    this.place = 0
                    this.$emit('failed', false)
                }
            }
        }
    }
}
</script>

<style scoped>
.dragVerify {
    border-width: 2px;
    border-style: solid;
}

.spout {
    position: relative;
}

.spout::before {
    content: '请按住滑块，拖动到最右边';
    top: 0;
    right: 0;
    bottom: 0;
    width: 100%;
    position: absolute;
    color: #919593;
    font-size: 16px;
    text-align: center;
}

.successTip {
    top: 0;
    right: 0;
    bottom: 0;
    width: 100%;
    height: 100%;
    position: absolute;
    color: #ffffff;
    font-size: 16px;
    text-align: center;
}

.succeedBox {
    color: #ffffff;
    font-size: 16px;
    text-align: center;
    width: 100%;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    position: absolute;
    background: #56e573;
    z-index: 8; 
}

.slidingBlock {
    height: calc(100% - 0.1rem);
    border-radius: 5px;
    border-top: none;
    border-left: none;
    background: #ffffff;
    top: 0;
    bottom: 0;
    left: 0;
    position: absolute;
    cursor: pointer;
    z-index: 9;
}

.blockIcon {
    content: '';
    background-size: 100% 100%;
    background-repeat: no-repeat;
    left: 50%;
    top: 50%;
    position: absolute;
    transform: translate(-50%, -50%);
    background-image: url("");
}
</style>